package cn.ffcs.uec.selfstaff.component;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Div;

import cn.ffcs.uec.common.manager.CustomFilesManager;
import cn.ffcs.uec.common.model.CustomFiles;
import cn.ffcs.uec.organization.manager.UecOrgOrgsManager;
import cn.ffcs.uec.organization.model.UecOrgOrgs;
import cn.ffcs.uec.selfstaff.component.bean.UecSelfStaffInfoListboxBean;
import cn.ffcs.uec.selfstaff.constant.UecSelfStaffConstants;
import cn.ffcs.uec.staff.manager.UecStaffInfoManager;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;
import lombok.Getter;
import lombok.Setter;

@Controller
@Scope("prototype")
public class UecSelfStaffInfoListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = -2212731640371254189L;

	/**
	 * 对应页面上的bean
	 */
	private UecSelfStaffInfoListboxBean bean = new UecSelfStaffInfoListboxBean();
	/**
	 * 人员基本信息对象
	 */
	private UecStaffInfo uecStaffInfo;

	private UecStaffInfoManager uecStaffInfoManager = (UecStaffInfoManager) ApplicationContextUtil
			.getBean("uecStaffInfoManager");
	
	private UecOrgOrgsManager uecOrgOrgsManager = (UecOrgOrgsManager) ApplicationContextUtil
			.getBean("uecOrgOrgsManager");

	private CustomFilesManager customFilesManager = (CustomFilesManager) ApplicationContextUtil
			.getBean("customFilesManager");

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecSelfStaffInfoListboxExt() {
		Executions.createComponents(
				UecSelfStaffConstants.UEC_STAFF_INFO_EXT_LISTBOX_ZUL, this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');
		
	}

	/**
	 * 初始化
	 * @throws Exception 
	 */
	public void onCreate() throws Exception {
		PubUtil.disableComponent(bean.getUecSelfStaffInfoListboxExtPanel());
		//获取登录用户ID
		//Long currentUserId = PlatformUtil.getCurrentUserId();
		//获取登录用户名
		String screenName = PlatformUtil.getCurrentUser().getScreenName().substring(1); //N123141
		String code = null;
		String userCode = screenName.substring(1);
		uecStaffInfo = uecStaffInfoManager.quaryUecStaffInfoByCode(userCode);
		if (screenName.indexOf("N") == -1) {
			code = screenName;
		} else{
			code = screenName.substring(1);
		}
		
		uecStaffInfo = uecStaffInfoManager.quaryUecStaffInfoByCode(code);
		if (uecStaffInfo != null) {
			PubUtil.fillBeanFromPo(uecStaffInfo, bean);
		}
		
		this.groupDataTranslationDisplay();
		Events.postEvent(UecSelfStaffConstants.ON_UEC_SELF_STAFF_QUERY, this,
				uecStaffInfo);
	}


	/**
	 * 绑定bean 集团数据翻译展示,并向其他组件跑出事件
	 * 
	 * @throws Exception
	 */
	public void groupDataTranslationDisplay() throws Exception {
		if (null != uecStaffInfo) {
			//String类型日期转Date
			this.stringTransToDate();
			// 进入电信来源
			if (null != uecStaffInfo.getGlbdef2()) {
				CustomFiles customFiles = new CustomFiles();
				customFiles.setPkDefdoc(uecStaffInfo.getGlbdef2());
				CustomFiles filesByCustomFiles = customFilesManager
						.findCustomFilesByCustomFiles(customFiles);
				if (null != filesByCustomFiles) {
					bean.getGlbdef2().setValue(filesByCustomFiles.getName());
				}
			}
			// 专业技术职务
			if (null != uecStaffInfo.getTitletechpost()) {
				CustomFiles customFiles = new CustomFiles();
				customFiles.setPkDefdoc(uecStaffInfo.getTitletechpost());
				CustomFiles filesByCustomFiles = customFilesManager
						.findCustomFilesByCustomFiles(customFiles);
				if (null != filesByCustomFiles) {
					bean.getTitletechpost().setValue(
							filesByCustomFiles.getName());
				}
			}
			// 职业资格
			if (null != uecStaffInfo.getProf()) {
				CustomFiles customFiles = new CustomFiles();
				customFiles.setPkDefdoc(uecStaffInfo.getProf());
				CustomFiles filesByCustomFiles = customFilesManager
						.findCustomFilesByCustomFiles(customFiles);
				if (null != filesByCustomFiles) {
					bean.getProf().setValue(filesByCustomFiles.getName());
				}
			}
			// 所属组织
			if (null != uecStaffInfo.getPkOrg()) {
				UecOrgOrgs org = new UecOrgOrgs();
				org.setPkOrg(uecStaffInfo.getPkOrg());
				UecOrgOrgs findUecOrgOrgsByUecOrgOrgs = uecOrgOrgsManager
						.findUecOrgOrgsByUecOrgOrgs(org);
				if (null != findUecOrgOrgsByUecOrgOrgs) {
					bean.getPkOrg().setValue(
							findUecOrgOrgsByUecOrgOrgs.getName());
				}
			}
			// 人力资源组织
			if (null != uecStaffInfo.getPkHrorg()) {
				UecOrgOrgs org = new UecOrgOrgs();
				org.setPkOrg(uecStaffInfo.getPkHrorg());
				UecOrgOrgs findUecOrgOrgsByUecOrgOrgs = uecOrgOrgsManager
						.findUecOrgOrgsByUecOrgOrgs(org);
				if (null != findUecOrgOrgsByUecOrgOrgs) {
					bean.getPkHrorg().setValue(
							findUecOrgOrgsByUecOrgOrgs.getName());
				}
			}
			// 证件类型
			if (null != uecStaffInfo.getIdtype()) {
				Long idtype = uecStaffInfo.getIdtype();
				if (0L == idtype) {
					bean.getIdtype().setValue("身份证");
				} else {
					bean.getIdtype().setValue("");
				}
			}
			// 性别
			if (null != uecStaffInfo.getSex()) {
				Long sex = uecStaffInfo.getSex();
				if (sex == 1) {
					bean.getSex().setValue("男");
				} else if (sex == 2) {
					bean.getSex().setValue("女");
				} else {
					bean.getSex().setValue("");
				}
			}
			// 民族
			if (null != uecStaffInfo.getNationality()) {
				String nationality = uecStaffInfo.getNationality();
				if ("1001A11000000000063W".equals(nationality)) {
					bean.getNationality().setValue("汉族");
				} else if ("1001A110000000000647".equals(nationality)) {
					bean.getNationality().setValue("侗族");
				} else if ("1001A11000000000063Y".equals(nationality)) {
					bean.getNationality().setValue("回族");
				} else if ("1001A110000000000646".equals(nationality)) {
					bean.getNationality().setValue("满族");
				} else if ("1001A11000000000063X".equals(nationality)) {
					bean.getNationality().setValue("蒙古族");
				}
			}

			// 婚姻状况
			if (null != uecStaffInfo.getMarital()) {
				String marital = uecStaffInfo.getMarital();
				if ("1001A1100000000005XQ".equals(marital)) {
					bean.getMarital().setValue("未婚");
				} else if ("1001A1100000000005XR".equals(marital)) {
					bean.getMarital().setValue("已婚");
				} else if ("1001A1100000000005XS".equals(marital)) {
					bean.getMarital().setValue("再婚");
				} else if ("1001A1100000000005XT".equals(marital)) {
					bean.getMarital().setValue("离异");
				} else if ("1001A1100000000005XU".equals(marital)) {
					bean.getMarital().setValue("丧偶");
				}
			}
			// 健康状况
			if (null != uecStaffInfo.getHealth()) {
				String health = uecStaffInfo.getHealth();
				if ("1001A1100000000005YD".equals(health)) {
					bean.getHealth().setValue("健康");
				} else if ("1001A1100000000005YE".equals(health)) {
					bean.getHealth().setValue("一般");
				} else if ("1001A1100000000005YF".equals(health)) {
					bean.getHealth().setValue("有慢性病");
				} else if ("1001A1100000000005YG".equals(health)) {
					bean.getHealth().setValue("有生理缺陷");
				} else if ("1001A1100000000005YH".equals(health)) {
					bean.getHealth().setValue("残疾");
				} else if ("1001A1100000000005YI".equals(health)) {
					bean.getHealth().setValue("较差");
				}
			}
			// 政治面貌
			if (null != uecStaffInfo.getPolity()) {
				String polity = uecStaffInfo.getPolity();
				if ("1001A1100000000006D6".equals(polity)) {
					bean.getPolity().setValue("中国共产党党员");
				} else if ("1001A1100000000006D7".equals(polity)) {
					bean.getPolity().setValue("中国共产党预备党员");
				} else if ("1001A1100000000006D8".equals(polity)) {
					bean.getPolity().setValue("中国共产主义青年团团员");
				} else if ("1001A1100000000006DI".equals(polity)) {
					bean.getPolity().setValue("群众");
				}
			}
			// 学历
			if (null != uecStaffInfo.getEdu()) {
				String edu = uecStaffInfo.getEdu();
				if ("1001A1100000000006AN".equals(edu)) {
					bean.getEdu().setValue("硕士研究生");
				} else if ("1001A1100000000006AO".equals(edu)) {
					bean.getEdu().setValue("研究生同等学历");
				} else if ("1001A1100000000006AP".equals(edu)) {
					bean.getEdu().setValue("大学本科");
				} else if ("1001A1100000000006AQ".equals(edu)) {
					bean.getEdu().setValue("专科");
				} else if ("1001A1100000000006AR".equals(edu)) {
					bean.getEdu().setValue("中专");
				} else if ("1001A1100000000006AS".equals(edu)) {
					bean.getEdu().setValue("高中");
				} else if ("1001A1100000000006AT".equals(edu)) {
					bean.getEdu().setValue("技工学校");
				} else if ("1001A1100000000006AU".equals(edu)) {
					bean.getEdu().setValue("初中");
				} else if ("1001A1100000000006AV".equals(edu)) {
					bean.getEdu().setValue("小学");
				} else if ("1001A1100000000006AW".equals(edu)) {
					bean.getEdu().setValue("无");
				} else if ("1001A1100000000006AX".equals(edu)) {
					bean.getEdu().setValue("大学普通班");
				}
			}
			// 学位
			if (null != uecStaffInfo.getPkDegree()) {
				String pkDegree = uecStaffInfo.getPkDegree();
				if ("1001A1100000000006AY".equals(pkDegree)) {
					bean.getPkDegree().setValue("博士");
				} else if ("1001A1100000000006AZ".equals(pkDegree)) {
					bean.getPkDegree().setValue("双硕士");
				} else if ("1001A1100000000006B0".equals(pkDegree)) {
					bean.getPkDegree().setValue("硕士");
				} else if ("1001A1100000000006B1".equals(pkDegree)) {
					bean.getPkDegree().setValue("双学士");
				} else if ("1001A1100000000006B2".equals(pkDegree)) {
					bean.getPkDegree().setValue("学士");
				} else if ("1001A1100000000006B3".equals(pkDegree)) {
					bean.getPkDegree().setValue("无");
				}
			}
			// 户口性质
			if (null != uecStaffInfo.getCharacterrpr()) {
				String characterrpr = uecStaffInfo.getCharacterrpr();
				if ("1001A1100000000005XM".equals(characterrpr)) {
					bean.getCharacterrpr().setValue("城镇");
				} else if ("1001A1100000000005XN".equals(characterrpr)) {
					bean.getCharacterrpr().setValue("农村");
				}
			}
			// 是否是历史成员
			if (null != uecStaffInfo.getIshiskeypsn()) {
				String ishiskeypsn = uecStaffInfo.getIshiskeypsn();
				if ("Y".equals(ishiskeypsn)) {
					bean.getIshiskeypsn().setValue("是");
				} else if ("N".equals(ishiskeypsn)) {
					bean.getIshiskeypsn().setValue("否");
				}
			}
			// 是否是门店营业员
			if (null != uecStaffInfo.getIsshopassist()) {
				String isshopassist = uecStaffInfo.getIsshopassist();
				if ("Y".equals(isshopassist)) {
					bean.getIsshopassist().setValue("是");
				} else if ("N".equals(isshopassist)) {
					bean.getIsshopassist().setValue("否");
				}
			}
			// 是否承包经营个人
			if (null != uecStaffInfo.getGlbdef14()) {
				String glbdef14 = uecStaffInfo.getGlbdef14();
				if ("Y".equals(glbdef14)) {
					bean.getGlbdef14().setValue("是");
				} else if ("N".equals(glbdef14)) {
					bean.getGlbdef14().setValue("否");
				}
			}
			// 启用状态
			if (null != uecStaffInfo.getEnablestate()) {
				Long enablestate = uecStaffInfo.getEnablestate();
				if (1L == enablestate) {
					bean.getEnablestate().setValue("未启用");
				} else if (2L == enablestate) {
					bean.getEnablestate().setValue("已启用");
				} else if (3L == enablestate) {
					bean.getEnablestate().setValue("已停用");
				} else {
					bean.getEnablestate().setValue("");
				}
			}
		}
	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 出生日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (null != uecStaffInfo.getBirthdate()) {
			bean.getBirthdate()
					.setValue(sdf.parse(uecStaffInfo.getBirthdate()));
		}
		// 入党(团)日期
		if (!StrUtil.isNullOrEmpty(uecStaffInfo.getJoinpolitydate())) {
			bean.getJoinpolitydate().setValue(
					sdf.parse(uecStaffInfo.getJoinpolitydate()));
		}
		// 身故日期
		if(!StrUtil.isNullOrEmpty(uecStaffInfo.getDieDate())){
			bean.getDieDate().setValue(sdf.parse(uecStaffInfo.getDieDate()));
		}
		// 参加工作日期
		if(!StrUtil.isNullOrEmpty(uecStaffInfo.getJoinworkdate())){
			bean.getJoinworkdate().setValue(sdf.parse(uecStaffInfo.getJoinworkdate()));
		}
		// 离退休日期
		if(!StrUtil.isNullOrEmpty(uecStaffInfo.getRetiredate())){
			bean.getRetiredate().setValue(sdf.parse(uecStaffInfo.getRetiredate()));
		}
		// 预计退休日期
		if(!StrUtil.isNullOrEmpty(uecStaffInfo.getGlbdef15())){
			bean.getGlbdef15().setValue(sdf.parse(uecStaffInfo.getGlbdef15()));
		}
		// 进入电信（邮电）系统日期
		if(!StrUtil.isNullOrEmpty(uecStaffInfo.getGlbdef3())){
			bean.getGlbdef3().setValue(sdf.parse(uecStaffInfo.getGlbdef3()));
		}
		
	}

}
